//
//  ProvinceViewController.m
//  shise
//
//  Created by Darling❤️ on 14/12/18.
//  Copyright (c) 2014年 MZP. All rights reserved.
//

#import "ProvinceViewController.h"
#import "City.h"
#import "NetHandler.h"


@interface ProvinceViewController ()

@end

@implementation ProvinceViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self handelData];
    }
    return self;
}


//处理数据
- (void)handelData
{
    NSString *str = @"https://api.shiseapp.com/v2/user/profile/location/?_city=嘉兴&_device=Matcha%20v2.5.0%20%28iPhone%206,%208.1.1%29&_version=2.5.0&conf_ver=1418876027&gps=38.883896,121.540677&sheng=%25E6%25B5%2599%25E6%25B1%259F&type=localcity&uuid=b611c1feab43d2ee1585a574f539263f";
    [NetHandler getDataWithUrl:str completion:^(NSData *data) {
        NSMutableDictionary *tempDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        //定义一个数组，存储解析后的数据
        NSArray *cityArr = [tempDic objectForKey:@"data"];
        //初始化这个数组
        self.arr = [NSMutableArray array];
        //遍历数组（存在字典中）
        for (NSMutableDictionary *dic in cityArr) {
            //定义个对象，把得到的值给对象赋值
            City *city = [[City alloc]initWithDictionary:dic];
            //把对象付给数组
            [self.arr addObject:city];
            [city release];
        }
       
        //刷新数据
        [self.tableView reloadData];
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"选择城市";
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667-49) style:UITableViewStylePlain];
    self.tableView.rowHeight = 50;
    self.tableView.delegate  =self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    
    
    
}


//两个协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reusese";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:str]autorelease];
        
    }
    City *city = [self.arr objectAtIndex:indexPath.row];
    cell.textLabel.text = city.title;

    return cell;
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
